英文還行的,推薦直接看教學影片,講得很清楚好懂!
Youtube 講解(全英)
SELECT cy.cityName, ct.customerName
FROM Customer as ct
INNER JOIN City cy
ON cy.cityId = ct.cityId
-- 由 JOIN 為分界,將 SQL 寫成一行左邊為 LEFT TABLE
SELECT * FROM `LEFT` LEFT JOIN `RIGHT`
-- 列出所有 CustomerId 不論其是否有對應的 cityId
SELECT *
FROM `Customer`
LEFT JOIN `City`
ON City.cityId = Customer.cityId
-- 此 SQL 效果同上,只是 TABLE 位置不同而已
-- 使用 RIGHT JOIN & 資料表位置對調
SELECT *
FROM `City`
Right JOIN `Customer`
ON City.cityId = Customer.cityId
SELECT *
FROM `Customer`
FULL JOIN `City`
ON City.cityId = Customer.cityId
SELECT * FROM `people`
WHERE id BETWEEN 1 AND 5
UNION ALL
SELECT * FROM `people`
WHERE id BETWEEN 3 AND 5
-- UNION ALL 取得 1~5 + 3~5 共8筆
-- UNION 取得 1~5 共5筆 重複的忽略